/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.rum.sender.senders;

import cz.muni.fi.rum.sender.command.RapRequest;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author pmikulasek
 */
public class BufferSender implements MainSender {

    private static BufferSender instance;
    private final List<RapRequest> requests;

    private BufferSender() {
        this.requests = Collections.synchronizedList(new LinkedList<RapRequest>());
    }

    public static BufferSender getInstance() {
        if (instance == null) {
            instance = new BufferSender();
        }
        return instance;
    }

    @Override
    public void addRequest(RapRequest request) {
        requests.add(request);
    }

    @Override
    public void removeRequest(RapRequest request) {
        if (requests.contains(request)) {
            requests.remove(request);
        }
    }

    @Override
    public void removeAllRequest() {
        requests.clear();
    }

    @Override
    public void sendAllRequests() {
        for (RapRequest request : requests) {
            request.execute();
        }
        removeAllRequest();
    }

    @Override
    public List<RapRequest> getAllRequests() {
        return Collections.unmodifiableList(requests);
    }
}
